package com.whfc.common.file;

import com.whfc.common.util.DateUtil;

import java.util.Date;

/**
 * @Description: 文件路径配置类
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-03-03 14:53
 */
public class FilePathConfig {

    private final static String FILE_PATH_FORMAT = "%s/%s/%s";

    private final static String FILE_PATH_FORMAT_HAS_DATE = "%s/%s/%s/%s/%s";

    private final static String FILE_PATH = "%s/%s.%s";

    /**
     * 路径前缀
     */
    private String prefix;

    /**
     * 应用环境
     */
    private String profile;

    public FilePathConfig(String prefix, String profile) {
        this.prefix = prefix;
        this.profile = profile;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    /**
     * 获取文件路径
     *
     * @param path 相对路径
     * @return prefix + "/" + profile + "/" + path
     */
    public String getFilePath(String path) {
        return String.format(FILE_PATH_FORMAT, prefix, profile, path);
    }

    /**
     * 获取文件路径
     *
     * @param path     相对路径
     * @param fileName 文件名
     * @return prefix + "/" + profile + "/" + path + "/" + fileName
     */
    public String getFilePath(String path, String fileName) {
        path = getFilePath(path);
        return String.format("%s/%s", path, fileName);
    }

    /**
     * 获取文件路径
     *
     * @param path
     * @param fileName
     * @param suffix
     * @return prefix + "/" + profile + "/" + path + "/" + fileName
     */
    public String getFilePath(String path, String fileName, String suffix) {
        path = getFilePath(path);
        return String.format(FILE_PATH, path, fileName, suffix);
    }

    /**
     * 获取文件路径
     *
     * @param path
     * @param deptId
     * @param date
     * @return
     */
    public String getFilePath(String path, Integer deptId, Date date) {
        String dateStr = DateUtil.formatDate(date, DateUtil.DATE_FORMAT);
        return String.format(FILE_PATH_FORMAT_HAS_DATE, prefix, profile, path, deptId, dateStr);
    }

    public static void main(String[] args) {

        FilePathConfig config = new FilePathConfig("ms", "dev");
        System.out.println(config.getFilePath("doc/file"));
        System.out.println(config.getFilePath("doc/export"));
        System.out.println(config.getFilePath("doc/file", 2, new Date()));
        System.out.println(config.getFilePath("abc/abc", "1234", "png"));
    }
}
